#include "Reader.h"
#include <sstream>

Reader::Reader() : maxBorrowCount(5), currentBorrowCount(0), isBlacklisted(false) {}

Reader::Reader(string id, string n, string g, string t, string p)
    : readerId(id), name(n), gender(g), type(t), phone(p),
    maxBorrowCount(5), currentBorrowCount(0), isBlacklisted(false) {
}

void Reader::displayInfo() const {
    cout << "读者号: " << readerId << endl;
    cout << "姓名: " << name << endl;
    cout << "性别: " << gender << endl;
    cout << "类型: " << type << endl;
    cout << "联系方式: " << phone << endl;
    cout << "最大借阅量: " << maxBorrowCount << endl;
    cout << "当前借阅量: " << currentBorrowCount << endl;
    cout << "黑名单状态: " << (isBlacklisted ? "是" : "否") << endl;
    cout << "----------------------------------------" << endl;
}

string Reader::toString() const {
    stringstream ss;
    ss << readerId << "|" << name << "|" << gender << "|"
        << type << "|" << phone << "|" << maxBorrowCount << "|"
        << currentBorrowCount << "|" << isBlacklisted;
    return ss.str();
}

Reader Reader::fromString(const string& data) {
    stringstream ss(data);
    string readerId, name, gender, type, phone;
    int maxBorrowCount, currentBorrowCount;
    bool isBlacklisted;

    getline(ss, readerId, '|');
    getline(ss, name, '|');
    getline(ss, gender, '|');
    getline(ss, type, '|');
    getline(ss, phone, '|');
    ss >> maxBorrowCount; ss.ignore();
    ss >> currentBorrowCount; ss.ignore();
    ss >> isBlacklisted;

    Reader reader(readerId, name, gender, type, phone);
    reader.setMaxBorrowCount(maxBorrowCount);
    reader.setCurrentBorrowCount(currentBorrowCount);
    reader.setIsBlacklisted(isBlacklisted);
    return reader;
}